#include <REG52.H>
#include "intrins.h"

sbit I2C_SCL = P0^1;
sbit I2C_SDA = P0^3;

void I2C_Start()   
{
    I2C_SCL = 0;
    I2C_SDA = 1;
    I2C_SCL = 1;
    _nop_();
    I2C_SDA = 0;
    _nop_();
}


void I2C_Stop()
{
    I2C_SCL = 0;
    I2C_SDA = 0;
    I2C_SCL = 1;
    _nop_();
    I2C_SDA = 1;
    _nop_();
}

void I2C_SendByte(unsigned char Byte){
    unsigned char i; 
    for(i=0;i<8;i++){
        I2C_SCL = 0;
        I2C_SDA = Byte&(0x80>>i);
        _nop_();
        I2C_SCL = 1;
        _nop_();
        I2C_SCL = 0;
        _nop_();
    }

}

unsigned char I2C_ReceByte(){
    unsigned char i, Byte=0x00;
    I2C_SDA = 1;
    for(i=0;i<8;i++){
        I2C_SCL = 1;
        if(I2C_SDA){ 
            Byte|=(0x80>>i);
        }
        I2C_SCL = 0;
    }
    return Byte;
}

void I2C_SendAck(unsigned char Byte){
    I2C_SDA = Byte;
    I2C_SCL = 1;
    I2C_SCL = 0;
}

unsigned char I2C_ReceAck(){
    unsigned char Byte;
    I2C_SDA = 1;
    _nop_();
    I2C_SCL = 1;
    _nop_();
    Byte = I2C_SDA;
    _nop_();
    I2C_SCL = 0;
    _nop_();
    return Byte;
}



